package com.wanli.agent.homepage.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wanli.agent.R;
import com.wanli.agent.bean.MerchantDetailsListBean;
import com.wanli.agent.homepage.MerchantDetailsActivity;
import com.wanli.agent.utils.DateTimeUtil;
import com.wanli.agent.utils.NumberFormatUtils;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 */
public class MerchantLookListAdapter extends RecyclerView.Adapter<MerchantLookListAdapter.ViewHolder> {

    private List<MerchantDetailsListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
    private String cycleType;

    public MerchantLookListAdapter(List<MerchantDetailsListBean.DataBean.ItemListBean> listsBeans, String cycleType) {
        this.listsBeans = listsBeans;
        this.cycleType = cycleType;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_merchant_look_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MerchantDetailsListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        holder.tvName.setText(dataBean.getName() + "(" + dataBean.getAgent_name() + ")");
        if (!TextUtils.isEmpty(dataBean.getSum_order_fee())) {
            String money = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getSum_order_fee()) / 100);
            holder.tvMoney.setText("交易总金额：" + money + "元");
            holder.tvMoney.setVisibility(View.VISIBLE);
        } else {
            holder.tvMoney.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(dataBean.getCreate_time())) {
            holder.tvCreateTime.setText("创建时间：" + DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getCreate_time()) * 1000)));
        } else {
            holder.tvCreateTime.setText("创建时间：");
        }
        if (!TextUtils.isEmpty(dataBean.getMobile())) {
            holder.tvMobile.setVisibility(View.VISIBLE);
            holder.tvMobile.setText("手机号码：" + dataBean.getMobile());
        } else {
            holder.tvMobile.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(cycleType)) {
            holder.tvManage.setVisibility(View.VISIBLE);
        } else {
            holder.tvManage.setVisibility(View.GONE);
        }

        if (dataBean.getChannel_type()==2){
            holder.tv_channel_type.setText("产品版本：智能版");
        }else {
            holder.tv_channel_type.setText("产品版本：标准版");
        }


        holder.itemView.setOnClickListener(v -> {
            if (TextUtils.isEmpty(cycleType)) {
                Intent intent = new Intent(context, MerchantDetailsActivity.class);
                intent.putExtra("agent_id", dataBean.getMerchant_id());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_mobile)
        TextView tvMobile;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_create_time)
        TextView tvCreateTime;
        @BindView(R.id.tv_manage)
        TextView tvManage;
        @BindView(R.id.tv_channel_type)
        TextView tv_channel_type;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
